package com.yf.dxapp.module.app.component.app;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.yf.dxapp.module.app.controller.admin.material.vo.MaterialPageReqVO;
import com.yf.dxapp.module.app.controller.app.vo.AppMaterialVO;
import com.yf.dxapp.module.app.controller.app.vo.ListResult;
import com.yf.dxapp.module.app.controller.app.vo.ProductListRequest;
import com.yf.dxapp.module.app.dal.dataobject.appuser.AppUserDO;
import com.yf.dxapp.module.app.dal.dataobject.material.MaterialDO;
import com.yf.dxapp.module.app.dal.mysql.appuser.AppUserMapper;
import com.yf.dxapp.module.app.enums.ErrorCodeConstants;
import com.yf.dxapp.module.app.enums.app.LevelEnum;
import com.yf.dxapp.module.app.service.category.CategoryService;
import com.yf.dxapp.module.app.service.material.MaterialService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 素材vo 处理器
 *
 * @author yxmds
 */
@Slf4j
@Component
public class AppMaterialComponent {


    @Resource
    private MaterialService appMaterialService;


    @Resource
    private QiniuFileComponent qiniuFileComponent;

    @Resource
    private CategoryService appCategoryService;

    private Integer Elastic_Title_TYPE = 1;

    private Integer LINE_TYPE = 1;

    private Integer Video_Title_TYPE = 1;

    public ListResult<AppMaterialVO> getKolProductList(Long loginUserId, ProductListRequest request) {
        MaterialPageReqVO query = buildQuery(loginUserId, request);
        PageResult<MaterialDO> pageResult = appMaterialService.getMaterialPage(query);
        List<MaterialDO> dxMaterials = pageResult.getList();
        if (dxMaterials.isEmpty()) {
            return new ListResult<>(Collections.emptyList(), false);
        }
        List<AppMaterialVO> appCourseVoList = dxMaterials.stream().map(dxMaterial -> {
            AppMaterialVO appMaterialVO = convertMaterial(dxMaterial);
            return appMaterialVO;
        }).collect(Collectors.toList());
        return new ListResult<>(appCourseVoList, request.getPage() * request.getLimit() <= pageResult.getTotal());
    }

    private AppMaterialVO convertMaterial(MaterialDO dxMaterial) {
        AppMaterialVO appMaterialVO = new AppMaterialVO();
        appMaterialVO.setAddTime(dxMaterial.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        appMaterialVO.setImages(Stream.of(dxMaterial.getCoverImgMaterialUrl().split(",")).collect(Collectors.toList()));
        appMaterialVO.setPid(dxMaterial.getId().toString());
        appMaterialVO.setProductId(dxMaterial.getProductId().toString());
        appMaterialVO.setProductType(dxMaterial.getProductType().toString());
        appMaterialVO.setTitle(dxMaterial.getTitle());
        appMaterialVO.setCover(dxMaterial.getCover());
        appMaterialVO.setUrl(dxMaterial.getUrl());
        appMaterialVO.setVideoUrl(dxMaterial.getVideoUrl());
        appMaterialVO.setPromotion(dxMaterial.getPromotion());
        appMaterialVO.setSimpleTitle(dxMaterial.getSimpleTitle());
        return appMaterialVO;
    }

    private MaterialPageReqVO buildQuery(Long loginUserId, ProductListRequest request) {
        MaterialPageReqVO materialPageReqVO = new MaterialPageReqVO();
        materialPageReqVO.setTitle(request.getKeywords());
        if (request.getType() != null && request.getType() > 0) {
            materialPageReqVO.setProductType(request.getType());
        }
        materialPageReqVO.setPageNo(request.getPage());
        materialPageReqVO.setPageSize(request.getLimit());
        return materialPageReqVO;
    }

    @Resource
    private AppUserMapper appUserMapper;

    public AppMaterialVO getDetail(Long loginUserId, Long pid) {
        AppUserDO userDO = appUserMapper.selectById(loginUserId);
        if(userDO==null||userDO.getLevelId().equals(LevelEnum.YK.levelId)){
            throw new ServiceException(ErrorCodeConstants.BUSINESS_ERROR.getCode(),"没有权限!");
        }
        MaterialDO material = appMaterialService.getMaterial(pid);
        return convertMaterial(material);
    }
}
